﻿using System;
using System.Diagnostics.Contracts;
using OpenLS.Spreadsheet.Expressions;

namespace OpenLS.Spreadsheet
{
    internal class WorksheetExpressionFormula :
        ExpressionFormula
    {
        private readonly Worksheet _worksheet;

        public WorksheetExpressionFormula(Expression expression, Worksheet worksheet) : base(expression)
        {
            Contract.Requires(expression != null);
            Contract.Requires(worksheet != null);
            _worksheet = worksheet;
        }

        protected override Worksheet Worksheet
        {
            get { return _worksheet; }
        }

        internal override object GetValue()
        {
            return Expression.GetValue(new WorksheetEvaluationContext(Worksheet));
        }

        public override CellFormulaBase Offset(CellRelativeAddress cellRelativeAddress)
        {
            return new WorksheetExpressionFormula(this.Expression.Offset(cellRelativeAddress),this.Worksheet);
        }
    }
}